import pandas as pd
import traceback
import re

pd.set_option('display.max_rows', None, 'display.max_columns', None, 'display.max_colwidth', 1000, 'display.expand_frame_repr', False)
xregexp = re.compile(r'\[[^\[\]\r\n\t]+\]')
xregexp_nchar6 = re.compile(r'\[[^\[\]\r\n\t]{,6}\]')


def get_trans_maps(xtrans_path):
    en2zh, zh2en = {}, {}
    with open(xtrans_path, 'r', encoding='utf8') as f:
        while True:
            xline = f.readline()
            if not xline:
                break
            if '\r\n' == xline[-2:]:
                xline = xline[:-2]
            elif '\n' == xline[-1:]:
                xline = xline[:-1]
            xen, xzh = xline.split('>')
            en2zh[xen] = xzh
            zh2en[xzh] = xen
    return en2zh, zh2en


if '__main__' == __name__:
    xtrans_path = r'D:\_const\svn\aliyun\cmpltrtok\content\python_nlp\zhuan_gao6\trans\tte_options_zh.trans.txt'
    en2zh, zh2en = get_trans_maps(xtrans_path)
    print(en2zh)
    print(zh2en)

    xcsv_path = r'D:\_dell7590_root\local\LNP_datasets\OCEMOTION-中文7分类细粒度情感分析数据集\OCEMOTION.csv'
    xwashed_path = r'_save\washed\emotion_fread_washed.txt'
    nchar2emoji = {}  # 带方括号的字数-to-带方括号的文本
    nchar2count = {}  # 带方括号的字数-to-这个字数的出现次数
    with open(xcsv_path, 'r', encoding='utf8') as fin:
        cnt = 0
        with open(xwashed_path, 'w', encoding='utf8') as fout:
            while True:
                xline = fin.readline()
                if not xline:
                    break
                if '\r\n' == xline[-2:]:
                    xline = xline[:-2]
                elif '\n' == xline[-1:]:
                    xline = xline[:-1]
                cnt += 1
                # if cnt > 20:
                #     break
                try:
                    xid, xtext, xem = xline.split('\t')
                    # print(xid, xtext, xem)
                    xid = int(xid)

                    # 检查方括号括住的文字
                    xmatches = xregexp.findall(xtext)
                    # print(xid, xmatches)
                    for xm in xmatches:
                        nchar = len(xm) - 2

                        # 维护nchar2emoji
                        xset = nchar2emoji.get(nchar, set())
                        xset.add(xm)
                        nchar2emoji[nchar] = xset

                        # 维护nchar2count
                        xcount = nchar2count.get(nchar, 0)
                        xcount += 1
                        nchar2count[nchar] = xcount

                    # 清除动图文本
                    xtext = re.sub(xregexp_nchar6, '', xtext)

                    xem = en2zh[xem]
                    fout.write(str(xid) + '\t' + xtext + '\t' + xem + '\n')
                except Exception as ex:
                    print(traceback.format_exc())
    print('Writing succ!')
    nchars = sorted(nchar2emoji.keys())
    for n in nchars:
        print(n, nchar2emoji[n])
    for n in nchars:
        print(n, nchar2count[n])
